from sklearn.datasets import load_breast_cancer
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
import pandas as pd
from sklearn.datasets import load_iris
import seaborn as sns
from sklearn.feature_selection import chi2

import matplotlib.pyplot as plt
dataset = load_breast_cancer()

X, y = load_iris(return_X_y=True)
# 卡方检验#
# 注意事项：特征数值必须非负
print("保留1个重要特征")
new_data = SelectKBest(chi2, k=1).fit_transform(X, y)
def correlation(X,y):
    # pvalues less ,more correlate
    scores, pvalues = chi2(X, y)
    print(pvalues)

"""
找出与类别最相关的特征
使用皮尔逊相关系数检查两个变量之间变化趋势的方向以及程度，值范围-1到+1，0表示两个变量不相关，正值表示正相关，负值表示负相关，值越大相关性越强
"""

df = pd.DataFrame(X)
df['y'] = y
corr= df.corr()

corr_y = abs(corr["y"])
print ("和y的相关性")
print (corr_y)
highest_corr = corr_y[corr_y > 0.1]       # 只看大于0.1的
highest_corr.sort_values(ascending=True)  # 发现只有3个特征与标签最相关

keep_features = highest_corr.sort_values(ascending=True).index[:-1]  # 去掉y

df_2 = df[corr_y.sort_values(ascending=True)[-11:-1].index]

plt.figure(figsize=(12, 10), dpi=80, facecolor='w', edgecolor='k')
corr_2 = df_2.corr()
sns.heatmap(corr_2, annot=True, fmt=".2g")
plt.show()